package com.ideaaedi.plugin.fastcoding.ui;

import com.alibaba.fastjson.JSON;
import com.ideaaedi.plugin.fastcoding.constant.FastCodingConstant;
import com.ideaaedi.plugin.fastcoding.util.JdPsiClassUtil;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.PsiType;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.util.ui.JBUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 请选择
 *
 * @author JustryDeng
 * @since 2022/1/6 23:45:49
 */
public class MockJson_ChooseAndShowJson extends JFrame {
    private JPanel jPanel;
    
    public MockJson_ChooseAndShowJson(Project project, java.util.List<PsiType> typeList) {
        // 初始化jPanel 并动态添加item
        initAndDynamicAddItem(project, typeList);
        
        // 设置内容面板
        setContentPane(jPanel);
        // 设置标题
        setTitle(FastCodingConstant.PLUGIN_NAME + FastCodingConstant.CONCAT_SIGN_WITH_WHITESPACE + FastCodingConstant.MOCK_JSON_NAME);
        
        // 设置大小 并 位置
        setSize(400, 50 * (typeList.size() + 1));
        setLocationRelativeTo(null);
    }
    
    /**
     * 初始化jPanel 并动态添加 item
     */
    private void initAndDynamicAddItem(Project project, java.util.List<PsiType> typeList) {
        jPanel = new JPanel();
        jPanel.setLayout(new GridLayoutManager(typeList.size() + 1, 1, JBUI.emptyInsets(), -1, -1));
        JLabel pleaseChooseTips = new JLabel();
        pleaseChooseTips.setText("识别到多个类，请选择其中一个进行mock");
        pleaseChooseTips.setIcon(Messages.getInformationIcon());
        jPanel.add(pleaseChooseTips, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST,
                GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW,
                GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        
        for (int i = 0; i < typeList.size(); i++) {
            JButton button = new JButton();
            final PsiType psiType = typeList.get(i);
            button.setText(psiType.getPresentableText());
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String mockedJson = JSON.toJSONString(JdPsiClassUtil.mockClass(project, psiType));
                    MockJson_ShowJson showJson = new MockJson_ShowJson(project, psiType.getPresentableText());
                    showJson.jsonArea.setText(mockedJson);
                    // 关闭当前选择框
                    dispose();
                    // 弹出json框
                    showJson.setVisible(true);
                }
            });
            
            jPanel.add(button, new GridConstraints(i + 1, 0, 1, 1, GridConstraints.ANCHOR_CENTER,
                    GridConstraints.FILL_HORIZONTAL,
                    GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                    GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        }
    }
    
}
